package com.shushan.pages.bean;

import com.android.net.bean.NetBaseBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: OutlineBean
 * @Desciption: 进度列表bean
 * @author: yichaohua
 * @date: 2018-04-23
 */
public class OutlineBean extends NetBaseBean {

    private static final String TAG = "OutlineBean";

    public int id;
    public int pid;
    public String outline_name;
    public int subject_id;
    public int level;
    public int state;//是否可练习 1 需要付费加锁  0 可练习
    public int score;

    public int chilidLevel = 1;
    private List<OutlineBean> childOutlineList = new ArrayList<OutlineBean>();

    public OutlineBean() {
    }

    public OutlineBean(int chilidLevel) {
        this.chilidLevel = chilidLevel;
    }

    @Override
    public void initByJson(JSONObject jsonObject) throws JSONException {
        id = jsonObject.optInt("id",0);
        pid = jsonObject.optInt("pid",-1);
        outline_name = jsonObject.optString("outline_name","");
        subject_id = jsonObject.optInt("subject_id",0);
        level = jsonObject.optInt("level",0);
        state = jsonObject.optInt("state",1);
        score = jsonObject.optInt("score",0);

        JSONArray childJsonArray = jsonObject.optJSONArray("children");


        if(childJsonArray != null && childJsonArray.length() > 0){
            for (int i = 0;i < childJsonArray.length();i++){
                JSONObject jsonObj = childJsonArray.optJSONObject(i);
                OutlineBean outlineBean = new OutlineBean(chilidLevel+1);
                outlineBean.initByJson(jsonObj);

                JSONArray jsonArray = jsonObj.optJSONArray("children");
                if((outlineBean.level == 1) || jsonArray.length() > 0){//过滤叶子
                    childOutlineList.add(outlineBean);
                }

            }
        }
    }

    public void getLeafList(List<OutlineBean> outlineList) {
        if(outlineList == null){
            outlineList = new ArrayList<OutlineBean>();
        }

        for (OutlineBean outlineBean : childOutlineList){
            outlineList.add(outlineBean);

            if(outlineBean.getOutlineList().size() > 0){
                outlineBean.getLeafList(outlineList);
            }

        }
    }

    public List<OutlineBean> getOutlineList() {
        return childOutlineList;
    }

    public void setOutlineList(List<OutlineBean> outlineList) {
        this.childOutlineList = outlineList;
    }
}
